package com.atguigu;

import io.minio.*;
import io.minio.errors.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class App {

    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {

        MinioClient minioClient = MinioClient.builder()
                .endpoint("http://192.168.253.128:9000")
                .credentials("minioadmin", "minioadmin")
                .build();
        boolean woca = minioClient.bucketExists(BucketExistsArgs.builder().bucket("woca").build());
        if (!woca) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket("woca").build());
            String policy = """
                        {
                          "Statement" : [ {
                            "Action" : "s3:GetObject",
                            "Effect" : "Allow",
                            "Principal" : "*",
                            "Resource" : "arn:aws:s3:::woca/*"
                          } ],
                          "Version" : "2012-10-17"
                        }""";
            minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket("woca").config(policy).build());
        } else {
            System.out.println("Bucket 'woca already exists.");
        }

        //上传图片
        minioClient.uploadObject(
                UploadObjectArgs.builder()
                        .bucket("woca")
                        .object("公寓-外观.jpg")
                        .filename("C:\\Users\\QA\\Desktop\\7.images\\公寓-外观.jpg")
                        .build());
        System.out.println("上传成功");
        }


    }

